//
//  XZMemberCheckMsgCell.m
//  Teecloud
//
//  Created by PC on 2018/11/18.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "XZMemberCheckMsgCell.h"
#import "GMPhotoCell.h"
#import "GMPlayerVoideCell.h"
#import "TZImagePickerController.h"
#import <KSPhotoBrowser/KSPhotoBrowser.h>
#import "GMLookPDFVC.h"
#import "XZMemberCheckMsg.h"
#import "GMVoiceListModel.h"
#import "GMAttachListModel.h"


@interface XZMemberCheckMsgCell ()<UICollectionViewDataSource,UICollectionViewDelegate,TZImagePickerControllerDelegate,UICollectionViewDelegateFlowLayout,UITableViewDataSource,UITableViewDelegate>

/*********************************Msg******************************/
@property (weak, nonatomic) IBOutlet UIImageView *msgImg;
@property (weak, nonatomic) IBOutlet UILabel *msgNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *msgTypeNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *msgValueLabel;
@property (weak, nonatomic) IBOutlet UILabel *msgCheckNameLabel;

@property (weak, nonatomic) IBOutlet UILabel *msgSendTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *msgLikeCountLabel;
@property (weak, nonatomic) IBOutlet UIImageView *msgLikeImg;
@property (weak, nonatomic) IBOutlet UIView *likeView;



@property (weak, nonatomic) IBOutlet UILabel *msgContentLabel;
@property (weak, nonatomic) IBOutlet UITableView *msgTableView;
@property (weak, nonatomic) IBOutlet UICollectionView *msgCollectionView;



/**********************************Quote***************************/
@property (weak, nonatomic) IBOutlet UIImageView *quoteImg;
@property (weak, nonatomic) IBOutlet UILabel *quoteNameLabel;

@property (weak, nonatomic) IBOutlet UILabel *quoteTypeLabel;
@property (weak, nonatomic) IBOutlet UILabel *quoteSendTimeLabel;

@property (weak, nonatomic) IBOutlet UILabel *quoteContentLabel;
@property (weak, nonatomic) IBOutlet UITableView *quoteTableView;
@property (weak, nonatomic) IBOutlet UICollectionView *quoteCollectionView;

/**************************NSLayoutConstraint************************/
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *msgViewHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *quoteViewHeight;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *msgTypeWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *quoteTypeWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *msgValueWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *msgTableViewHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *msgCollectionViewHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *quoteTableViewHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *quoteCollectionViewHeight;

@end
#define BorderColor [UIColor colorWithHexString:@"#FF7708" andAlpha:1.0]
@implementation XZMemberCheckMsgCell
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    /************Msg Quote 视图的相关view的属性设置**************/
    self.msgTypeNameLabel.layer.borderColor = BorderColor.CGColor;
    self.msgTypeNameLabel.layer.borderWidth = 1.0f;
    self.msgTypeNameLabel.layer.cornerRadius = 2.0f;
    self.msgTypeNameLabel.clipsToBounds = YES;
    //等级
    self.msgValueLabel.layer.cornerRadius = 2.0f;
    self.msgValueLabel.clipsToBounds = YES;

    //quoteTypeLabel
    self.quoteTypeLabel.layer.borderColor = BorderColor.CGColor;
    self.quoteTypeLabel.layer.borderWidth = 1.0f;
    self.quoteTypeLabel.layer.cornerRadius = 2.0f;
    self.quoteTypeLabel.clipsToBounds = YES;
    
    //TableView的属性设置
    self.msgTableView.delegate = self;
    self.msgTableView.dataSource = self;
    self.msgTableView.scrollEnabled = false;
    [self.msgTableView registerNib:[UINib nibWithNibName:@"GMPlayerVoideCell" bundle:nil] forCellReuseIdentifier:@"GMPlayerVoideCell"];

    self.quoteTableView.delegate = self;
    self.quoteTableView.dataSource = self;
    self.quoteTableView.scrollEnabled = false;
    [self.quoteTableView registerNib:[UINib nibWithNibName:@"GMPlayerVoideCell" bundle:nil] forCellReuseIdentifier:@"GMPlayerVoideCell"];
    
    //Collectionview的属性设置
    self.msgCollectionView.scrollEnabled = false;
    self.msgCollectionView.backgroundColor = kWhiteColor;
    self.msgCollectionView.dataSource = self;
    self.msgCollectionView.delegate = self;
    [self.msgCollectionView registerNib:[UINib nibWithNibName:@"GMPhotoCell" bundle:nil] forCellWithReuseIdentifier:@"GMPhotoCell"];
    
    self.quoteCollectionView.scrollEnabled = false;
    self.quoteCollectionView.backgroundColor = kEAEAEAColor;
    self.quoteCollectionView.dataSource = self;
    self.quoteCollectionView.delegate = self;
    [self.quoteCollectionView registerNib:[UINib nibWithNibName:@"GMPhotoCell" bundle:nil] forCellWithReuseIdentifier:@"GMPhotoCell"];
    
    //点赞view添加手势
    UITapGestureRecognizer *likeTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(likeTapAction)];
    [self.likeView addGestureRecognizer:likeTap];
}
- (void)setModel:(XZMemberCheckMsg *)model{
    _model = model;
    /*********************MsgBoby*****************/
    [self.msgImg yy_setImageWithURL:[NSURL URLWithString:model.MsgBody.UserInfo.HeadUrl] placeholder:DefealtImg];
    self.msgNameLabel.text = model.MsgBody.UserInfo.UserName;
    self.msgSendTimeLabel.text = model.MsgBody.SendTime;
    //评论
    self.msgContentLabel.text = model.MsgBody.Content;
    CGFloat msgContentHight = [self.msgContentLabel getLableSizeWithMaxWidth:(DEVICE_WIDTH-59)].height;
    
    //MsgBody.MsgTypeName（为空不显示）
    if (model.MsgBody.MsgTypeName != nil) {
        self.msgTypeNameLabel.hidden = false;
        self.msgTypeNameLabel.text = model.MsgBody.MsgTypeName;
        CGSize titleSize = [model.MsgBody.MsgTypeName boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.msgTypeNameLabel.font} context:nil].size;
        self.msgTypeWidth.constant =titleSize.width+8;
    }else{
        self.msgTypeNameLabel.hidden = true;
        self.msgTypeWidth.constant = 0;
    }
    
    //MsgBody.ValueGrade为空msgValueLabel不显示
    if (model.MsgBody.ValueGrade != nil) {
        //MsgBody.ValueType=1显示：优（1）、良（2）、中（3）、差（4），优良用蓝色k0065EAColor，中差用橙色BorderColor；
        if ([model.MsgBody.ValueType isEqualToNumber:@(1)]) {
            if ([model.MsgBody.ValueGrade isEqualToString:@"1"]) {
                self.msgValueLabel.backgroundColor =k0065EAColor;
                self.msgValueLabel.text = @"优";
            }else if ([model.MsgBody.ValueGrade isEqualToString:@"2"]){
                self.msgValueLabel.backgroundColor =k0065EAColor;
                self.msgValueLabel.text = @"良";
            }else if ([model.MsgBody.ValueGrade isEqualToString:@"3"]){
                self.msgValueLabel.backgroundColor =BorderColor;
                self.msgValueLabel.text = @"中";
            }else if ([model.MsgBody.ValueGrade isEqualToString:@"4"]){
                self.msgValueLabel.backgroundColor =BorderColor;
                self.msgValueLabel.text = @"差";
            }
        }else if ([model.MsgBody.ValueType isEqualToNumber:@(2)]){
            //MsgBody.ValueType=2，显示：MsgBody.ValueGrade+分，>=60分用蓝色，<60分用橙色
            if ([model.MsgBody.ValueGrade integerValue] >= 60) {
                self.msgValueLabel.backgroundColor =k0065EAColor;
                self.msgValueLabel.text = [NSString stringWithFormat:@"%@分",model.MsgBody.ValueGrade];
            }else{
                self.msgValueLabel.backgroundColor =BorderColor;
                self.msgValueLabel.text = [NSString stringWithFormat:@"%@分",model.MsgBody.ValueGrade];
            }
        }
        CGSize size = [self.msgValueLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, 15) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.msgValueLabel.font} context:nil].size;
        self.msgValueWidth.constant =size.width+8;
    }else{
        self.msgValueLabel.hidden = true;
        self.msgValueWidth.constant = 0;
    }
    //MsgBody.CheckName（为空不显示）
    if (model.MsgBody.CheckName != nil) {
        self.msgCheckNameLabel.hidden = false;
        self.msgCheckNameLabel.text = model.MsgBody.CheckName;
    }else{
        self.msgCheckNameLabel.hidden = true;
    }
    
    //点赞
    self.msgLikeCountLabel.text = [NSString stringWithFormat:@"%@",model.MsgBody.LikeCount];
    if (!model.MsgBody.IsLike) {//是否已经点赞
        self.msgLikeImg.image = [UIImage imageNamed:@"finger-gray-64"];
    }else{
        self.msgLikeImg.image = [UIImage imageNamed:@"finger-blue-64"];
    }
   
    //语音
    if (model.MsgBody.VoiceList.count == 0) {
        self.msgTableViewHeight.constant = 0;
        self.msgTableView.hidden = true;
    }else{
        self.msgTableViewHeight.constant = model.MsgBody.VoiceList.count *30;
        self.msgTableView.hidden = false;
        [self.msgTableView reloadData];
    }
    //图片
    if (model.MsgBody.ImgFileList.count == 0) {
        self.msgCollectionViewHeight.constant = 0;
        self.msgCollectionView.hidden = true;
    }else{
        self.msgCollectionViewHeight.constant = 60;
        self.msgCollectionView.hidden = false;
        [self.msgCollectionView reloadData];
    }
    self.msgViewHeight.constant = 58+msgContentHight+self.msgTableViewHeight.constant+self.msgCollectionViewHeight.constant+15;
    
    
    /*******************************Quote*******************/
    self.quoteImg.hidden = false;
    self.quoteNameLabel.hidden = false;
    self.quoteSendTimeLabel.hidden = false;
    self.quoteTypeLabel.hidden = false;
    [self.quoteImg yy_setImageWithURL:[NSURL URLWithString:model.Quote.UserInfo.HeadUrl] placeholder:DefealtImg];
    self.quoteNameLabel.text = model.Quote.UserInfo.UserName;
    self.quoteSendTimeLabel.text = model.Quote.SendTime;
    //评论
    self.quoteContentLabel.text = model.Quote.Content;
    CGFloat quoteContentHight = [self.quoteContentLabel getLableSizeWithMaxWidth:(DEVICE_WIDTH-113)].height;
    
    self.quoteTypeLabel.text = model.Quote.MsgTypeName;
    //类别的宽度
    CGSize size = [self.quoteTypeLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, 15) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.quoteTypeLabel.font} context:nil].size;
    self.quoteTypeWidth.constant =size.width+8;
    //语音
    if (model.Quote != nil) {
        if (model.Quote.VoiceList.count == 0) {
            self.quoteTableViewHeight.constant = 0;
            self.quoteTableView.hidden = true;
        }else{
            self.quoteTableViewHeight.constant = model.Quote.VoiceList.count *30;
            self.quoteTableView.hidden = false;
            [self.quoteTableView reloadData];
        }
        
        if (model.Quote.ImgFileList.count == 0) {
            self.quoteCollectionViewHeight.constant = 0;
            self.quoteCollectionView.hidden = true;
        }else{
            self.quoteCollectionViewHeight.constant = 60;
            self.quoteCollectionView.hidden = false;
            [self.quoteCollectionView reloadData];
        }
        self.quoteViewHeight.constant = 47+quoteContentHight+self.quoteTableViewHeight.constant+self.quoteCollectionViewHeight.constant+6;
    }else{
        //隐藏Quote的相应控件
        self.quoteImg.hidden = true;
        self.quoteNameLabel.hidden = true;
        self.quoteSendTimeLabel.hidden = true;
        self.quoteTypeLabel.hidden = true;
        
        self.quoteTableViewHeight.constant = 0;
        self.quoteTableView.hidden = true;
        
        self.quoteCollectionViewHeight.constant = 0;
        self.quoteCollectionView.hidden = true;
        self.quoteViewHeight.constant = 0;
        
    }
    
    //cell总高度
    if (self.quoteViewHeight.constant == 0) {
        model.cellHeight = self.msgViewHeight.constant + self.quoteViewHeight.constant+1;
    }else{
        model.cellHeight = self.msgViewHeight.constant + self.quoteViewHeight.constant+6;
    }
}
#pragma mark - CollectViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if ([collectionView isEqual:self.msgCollectionView]) {
        return self.model.MsgBody.ImgFileList.count;
    }
    return self.model.Quote.ImgFileList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GMPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GMPhotoCell" forIndexPath:indexPath];
    GMAttachListModel *model= [[GMAttachListModel alloc]init];
    if ([collectionView isEqual:self.msgCollectionView]) {
        model= self.model.MsgBody.ImgFileList[indexPath.row];
    }else{
        model= self.model.Quote.ImgFileList[indexPath.row];
    }
    
    if ([model.FileType isEqualToNumber:@(3)]) {
        [cell.photoImg yy_setImageWithURL:[NSURL URLWithString:model.Url] placeholder:DefealtImg];
    }else if ([model.FileType isEqualToNumber:@(4)]){
        cell.photoImg.image =[UIImage imageNamed:@"doc-pdf"];
    }else if ([model.FileType isEqualToNumber:@(5)]){
        cell.photoImg.image =[UIImage imageNamed:@"doc-ppt"];
    }else if ([model.FileType isEqualToNumber:@(6)]){
        cell.photoImg.image =[UIImage imageNamed:@"doc-word"];
    }else if ([model.FileType isEqualToNumber:@(7)]){
        cell.photoImg.image =[UIImage imageNamed:@"doc-excel"];
    }else if ([model.FileType isEqualToNumber:@(8)]){
        cell.photoImg.image =[UIImage imageNamed:@"doc-rar"];
    }else if ([model.FileType isEqualToNumber:@(9)]){
        cell.photoImg.image =[UIImage imageNamed:@"doc-zip"];
    }else{
        cell.photoImg.image =[UIImage imageNamed:@"doc-other"];
    }
    cell.delectBtn.hidden=YES;
    return cell;
}
//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((DEVICE_WIDTH-88-10-10-40)/5, (DEVICE_WIDTH-88-10-10-40)/5);
}

//设置每个item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 0, 10,4);
}

//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 4;
}
//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 4;
}
//点击item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if ([collectionView isEqual:self.msgCollectionView]) {
        /**放大图片*/
        GMAttachListModel *fileModel= self.model.MsgBody.ImgFileList[indexPath.row];
        if ([fileModel.FileType isEqualToNumber:@(3)]) {
            GMPhotoCell * cell = (GMPhotoCell *)[self collectionView:collectionView cellForItemAtIndexPath:indexPath];//即为要得到的cell
            NSMutableArray *items = @[].mutableCopy;
            NSInteger count = self.model.MsgBody.ImgFileList.count;
            for (int i = 0; i < count; i++) {
                GMAttachListModel *model= self.model.MsgBody.ImgFileList[i];
                if ([model.FileType isEqualToNumber:@(3)]) {
                    UIImageView *imgV = cell.photoImg;
                    KSPhotoItem *item = [KSPhotoItem itemWithSourceView:imgV imageUrl:[NSURL URLWithString:model.Url]];
                    [items addObject:item];
                }
            }
            KSPhotoBrowser *browser = [KSPhotoBrowser browserWithPhotoItems:items selectedIndex:indexPath.row];
            browser.dismissalStyle = KSPhotoBrowserInteractiveDismissalStyleRotation;
            browser.backgroundStyle = KSPhotoBrowserBackgroundStyleBlurPhoto;
            browser.loadingStyle = KSPhotoBrowserImageLoadingStyleDeterminate;
            browser.pageindicatorStyle = KSPhotoBrowserPageIndicatorStyleDot;
            browser.bounces = NO;
            [browser showFromViewController:self.parentController];
        }else{
            GMLookPDFVC *preview = [[GMLookPDFVC alloc] init];
            preview.model = fileModel;
            [self.parentController.navigationController pushViewController:preview animated:YES];
        }
    }else{
        /**放大图片*/
        GMAttachListModel *fileModel=
        self.model.Quote.ImgFileList[indexPath.row];
        if ([fileModel.FileType isEqualToNumber:@(3)]) {
            GMPhotoCell * cell = (GMPhotoCell *)[self collectionView:collectionView cellForItemAtIndexPath:indexPath];//即为要得到的cell
            NSMutableArray *items = @[].mutableCopy;
            NSInteger count = self.model.Quote.ImgFileList.count;
            for (int i = 0; i < count; i++) {
                GMAttachListModel *model= self.model.Quote.ImgFileList[i];
                if ([model.FileType isEqualToNumber:@(3)]) {
                    UIImageView *imgV = cell.photoImg;
                    KSPhotoItem *item = [KSPhotoItem itemWithSourceView:imgV imageUrl:[NSURL URLWithString:model.Url]];
                    [items addObject:item];
                }
            }
            KSPhotoBrowser *browser = [KSPhotoBrowser browserWithPhotoItems:items selectedIndex:indexPath.row];
            browser.dismissalStyle = KSPhotoBrowserInteractiveDismissalStyleRotation;
            browser.backgroundStyle = KSPhotoBrowserBackgroundStyleBlurPhoto;
            browser.loadingStyle = KSPhotoBrowserImageLoadingStyleDeterminate;
            browser.pageindicatorStyle = KSPhotoBrowserPageIndicatorStyleDot;
            browser.bounces = NO;
            [browser showFromViewController:self.parentController];
        }else{
            GMLookPDFVC *preview = [[GMLookPDFVC alloc] init];
            preview.model = fileModel;
            [self.parentController.navigationController pushViewController:preview animated:YES];
        }
    }
}

#pragma mark - TableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([tableView isEqual:self.msgTableView]) {
        return  self.model.MsgBody.VoiceList.count;
    }
    return self.model.Quote.VoiceList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    kWeakObject(self)
    NSString *voiceStr = nil;//msg quote 的语音标识
    GMPlayerVoideCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GMPlayerVoideCell" forIndexPath:indexPath];
    GMVoiceListModel *model = [[GMVoiceListModel alloc]init];
    if ([tableView isEqual:self.msgTableView]) {
        voiceStr = @"MSG";
        cell.contentView.backgroundColor = kWhiteColor;
        model = self.model.MsgBody.VoiceList[indexPath.row];
    }else{
        voiceStr = @"QUOTE";
        cell.contentView.backgroundColor = kEAEAEAColor;
        model = self.model.Quote.VoiceList[indexPath.row];
    }
    cell.model = model;
    //语音读与未读
    cell.readBlock = ^{
        if (weakself.hasReadBlock) {
            weakself.hasReadBlock(indexPath.row, model.Id,voiceStr);
        }
    };
    return cell;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30.f;
}

//点赞
- (void)likeTapAction{
    if (self.likeBlock) {
        self.likeBlock(self.model,self.indexPath);
    }
}


@end
